package com.project.yuepai.util;


import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.*;

public class QqMsgUtil {
    //hzu-lai开发者密钥，请勿外传
    public static final String ACCESS_KEY_ID = "AKIDAXnpdGguteASVJsCJH1XqvlNmxtqZ4KN";
    public static final String ACCESS_KEY_SECRET = "rTqz56wbEoFYjqmUxBBzZu0rTrzsWv57";
    //短信签名,可在腾讯云短信控制台找到
    public static final String SIGN_NAME = "惠大创协";
    //创建应用的appId
    public static final String SMS_SDK_APP_ID = "1400587460";


    //注册时的验证码
    public static boolean sendCode(String phone,String code,String templeteId) {
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {phone};
            req.setPhoneNumberSet(phoneNumberSet1);

            //创建应用的appId
            req.setSmsSdkAppId(SMS_SDK_APP_ID);
            req.setSignName(SIGN_NAME);
            //短信变量模板，
            req.setTemplateId(templeteId);

            String[] templateParamSet1 = {code};
            req.setTemplateParamSet(templateParamSet1);

            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(resp));
            if(resp.getSendStatusSet()[0].getCode().equals("Ok")){
                System.out.println("message:"+resp.getSendStatusSet()[0].getMessage()+"\nRequestId:"+resp.getRequestId());
                return true;
            }
            return false;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            return false;
        }
    }

    /**
     * 发送短信到用户的入口，templateId是根据用户的需求选择对应的短信模板
     * 这里的需求包括登录验证码，修改注册手机验证码，密码重置，注册验证码等，目前就这四种
     * @param phone
     * @param code
     * @param templateId
     */
    public static String sendMsg2User(String phone,String code,int templateId){
        //四种需求，范围是0-3
        if(templateId>4)
            return "Invalid templateId!";
        if (sendCode(phone,code,getTempleteId(templateId)))
            return "success";
        return "failure";
    }

    public static String getTempleteId(int templeteId){
        switch (templeteId){
            case 0:
                return "1171004";  //登录验证码模板
            case 1:
                return "1171005";  //修改注册手机验证码模板
            case 2:
                return "1171007";  //密码重置模板
            case 3:
                return "1174123";  //注册验证码模板
        }
        return null;
    }

}
